package net.homelinux.chaoswg.io.renga.core.gui.operations;

import javax.swing.JOptionPane;

import net.homelinux.chaoswg.io.renga.core.gui.DrawingArea;
import net.homelinux.chaoswg.io.renga.core.gui.operations.helper.Connection;
import net.homelinux.chaoswg.io.renga.core.gui.operations.visualisations.NamedBlockVisualisation;

import org.jdom.Element;

public class ConstantOperation extends Operation {
    Object toReturn = null;
    
    public ConstantOperation () {
        this.visualisation = new NamedBlockVisualisation(0,0,32,32, "");
        this.outputs = 1;
        this.inputs = 0;
        this.connections = new Connection[0];
        this.visualisation.setInputs(inputs);
        this.visualisation.setOutputs(outputs);
    }

    @Override
    public Object apply(final int i) {
        if (i == 0) {
            return toReturn;
        }

        return null;
    }

    @Override
    public void doubleClick(final DrawingArea dv) {
        Object name = toReturn;
        
        do {
            name = JOptionPane.showInputDialog(dv, "Please enter name for binding", name);
        } while (name == null || name.equals(""));
        
        ((NamedBlockVisualisation)this.visualisation).setName(name.toString());
        this.toReturn = name;
    }

    @Override
    public String getDoubleClickActionName() {
        return "Set value";
    }
    
    @Override
    public Element writeYourself () {
        final Element toReturn = super.writeYourself();
        
        if (this.toReturn != null) {
            final Element returnValueElement = new Element ("returnValue");
            returnValueElement.addContent(this.toReturn.toString());
            toReturn.addContent(returnValueElement);
        }
        return toReturn;
    }

    public Object getToReturn() {
        return toReturn;
    }

    public void setToReturn(final Object toReturn) {
        this.toReturn = toReturn;
        ((NamedBlockVisualisation)this.visualisation).setName(toReturn.toString());
    }
}
